package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemDao;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class checkItemServiceImpl implements CheckItemService {
  //注入mapper对象
       @Autowired
       private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {

        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();//获取当前页码
        Integer pageSize = queryPageBean.getPageSize (); //获取每页页数
        String queryString = queryPageBean.getQueryString ();//获取查询条件 ,封装在queryPageBean
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        //select * from t_checkitem limit 0,10
        PageHelper.startPage(currentPage,pageSize);
        //查询条件
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        //前台传给后台的
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total,rows);
    }
    /*根据id查询检查项表单*/
    @Override
    public void deleteById(Integer id) {
   //判断当前检查项是否已经关联到检查组
      long count = checkItemDao.findCountByCheckItemId(id);
        if (count >0 ){
            //当前检查项已经被关联到检查组，不允许删除
            throw new RuntimeException ();
        }
        checkItemDao.deleteById(id);
    }
  /*修改检查项*/
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit ( checkItem );
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }
    /*查询所有检查项*/
    @Override
    public List <CheckItem> findAll() {
        return checkItemDao.findAll();
    }

}
